
#import "JYTabBarController.h"

#import "JYTabBarButton.h"
#import "MaiVC.h"


@interface JYTabBarController () <JYTabBarDelegate>

@property (nonatomic, weak) JYTabBar *customTabBar;
@property (nonatomic, weak) UIButton *homeBtn;
@property(nonatomic,assign) NSInteger lastIndex;

@end

@implementation JYTabBarController

#pragma mark - 初始化方法
- (JYTabBar *)customTabBar
{
    if (!_customTabBar) {
        // 创建一个新的tabbar
        JYTabBar *customTabBar = [[JYTabBar alloc] init];
        CGRect frame = CGRectMake(0, mainScreenHeight-55, mainScreenWidth, 55);
      //  CGRect frame = self.tabBar.bounds;
        NSLog(@"y = %.1f,h = %.1f, sh=%.1f",frame.origin.y,frame.size.height,mainScreenHeight);
        customTabBar.frame = frame;

        customTabBar.delegate = self;
        //[self.tabBar addSubview:customTabBar];
        [self.view addSubview:customTabBar];
        self.customTabBar = customTabBar;
        self.lastIndex = 0;
    }
    return _customTabBar;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //初始化所有子控制器
    [self setupAllChildVCs];
    //添加首页按钮
 //liurg   [self setupHomeButton];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    // 移除系统自动产生的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
    [self.tabBar removeFromSuperview];
}

-(void)hideSysTabar{
    // 移除系统自动产生的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
    [self.tabBar removeFromSuperview];
}

/**
 *  初始化所有子控制器
 */
- (void)setupAllChildVCs
{
    
    for (NSInteger i = 0; i < 4; i++) {
        if(i==0){
            MaiVC *homeCtrl = [[MaiVC alloc] init];
            [self setupOneChildVC:homeCtrl title:@"首页" imageName:@"tab_home.png" selectedImageName:@"tab_home_select.png"];
          //  navigate = [[UINavigationController alloc] initWithRootViewController:mainContr];
 
        }
        else if (i==1){
            MaiVC *kmusicViewCtr = [[MaiVC alloc]init];
            [self setupOneChildVC:kmusicViewCtr title:@"点歌" imageName:@"tab_search.png" selectedImageName:@"tab_search_select.png"];
       //     navigate = [[UINavigationController alloc] initWithRootViewController:SearCtr];
            
        }
        else if (i==2){
            MaiVC *relationshipViewCtr = [[MaiVC alloc] init];
            [self setupOneChildVC:relationshipViewCtr title:@"会员中心" imageName:@"tab_shopcart.png" selectedImageName:@"tab_shopcart_select.png"];
        
        }
      
        else if (i==3){
            MaiVC *forMeViewCtr = [[MaiVC alloc] init];
            [self setupOneChildVC:forMeViewCtr title:@"会员中心" imageName:@"tab_me.png" selectedImageName:@"tab_me_select.png"];
            
        }
    }
    

}
/**
 *  初始化一个子控制器
 */
- (void)setupOneChildVC:(UIViewController *)child title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.设置tabBarItem
    child.title = title;
    child.tabBarItem.image = [UIImage imageNamed:imageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    if (iOS7) {
        selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    child.tabBarItem.selectedImage = selectedImage;
    // 2.添加子控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:child];
    [self addChildViewController:nav];
    [nav.navigationBar setBarTintColor:JYColor(255, 0, 44)];
   // [nav.navigationBar setBarTintColor:[UIColor clearColor]];

    [nav.navigationBar setTintColor:[UIColor whiteColor]];
    NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                               [UIColor whiteColor],NSForegroundColorAttributeName,
                                               nil];
    [[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [nav.navigationBar setTranslucent:NO];

    // 3.往tabbar里面添加选项卡按钮
    [self.customTabBar addTabBarButton:child.tabBarItem];
}
/**
 *  添加首页按钮
 */
- (void)setupHomeButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.width = self.tabBar.width / 5 * 0.8;
    button.height = button.width;
    button.centerX = self.tabBar.centerX;
    button.y = self.customTabBar.height - button.height - 5;
    button.adjustsImageWhenHighlighted = NO;
    [button setBackgroundImage:[UIImage imageNamed:@"tabbar_home"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"tabbar_home_selected"] forState:UIControlStateDisabled];
    [button addTarget:self action:@selector(clickHome) forControlEvents:UIControlEventTouchUpInside];
    [self.customTabBar addSubview:button];
    self.homeBtn = button;
}
- (void)clickHome
{
    self.homeBtn.enabled = NO;
    self.selectedIndex = 0;
    [self.customTabBar selectedHome];
}

- (void)tabBar:(JYTabBar *)tabBar didSelectButtonFrom:(long)from to:(long)to
{
    self.selectedIndex = to-100;
    self.lastIndex = from-100;
    if (self.homeBtn.enabled == NO) {
        self.homeBtn.enabled = YES;
    }
}

-(void)setSelectedIndexView:(NSInteger)whichView{
    
    JYTabBarButton *selectBtn =  (JYTabBarButton *)[self.customTabBar viewWithTag:whichView+100];
    selectBtn.enabled = NO;
    JYTabBarButton *lastBtn = (JYTabBarButton *)[self.customTabBar viewWithTag:self.selectedIndex+100];
    lastBtn.enabled = YES;
    long tempIndex = self.selectedIndex;
    self.lastIndex = tempIndex;
    self.selectedIndex = whichView;
    self.customTabBar.selectedTabBarButton = selectBtn;
}



-(void)backLastIndexView{
    
    JYTabBarButton *lastBtn =  (JYTabBarButton *)[self.customTabBar viewWithTag:self.lastIndex+100];
    lastBtn.enabled = NO;
    JYTabBarButton *selectBtn = (JYTabBarButton *)[self.customTabBar viewWithTag:self.selectedIndex+100];
    selectBtn.enabled = YES;
    long tempIndex = self.selectedIndex;
    self.selectedIndex = self.lastIndex;
    self.lastIndex = tempIndex;
    self.customTabBar.selectedTabBarButton = lastBtn;
}

- (void)setToNewPoint:(NSInteger)which IsHid:(BOOL)toHid{
    if (which < MAXNEWPOINT) {
        NSString *pointStr = [NSString stringWithFormat:@"point%ld",(long)which];
        UIView *pointView =[_customTabBar.pointMuDic objectForKey:pointStr];
      if(pointView)
      {
          pointView.hidden = toHid;
      }
        
    }
}


@end
